package reconnaissance.traitement.impl;

import librairies.liste.interfaces.IIterateur;
import librairies.liste.interfaces.IListeE;
import reconnaissance.traitement.interfaces.ITraitement;
import utils.PropertiesUtils;

import commande.outils.Processus;

import enumeration.type.ETypeExecution;

/**
 * gestion du traitemnt des demandes d execution.
 * 
 * @author mlozach
 */
public class TraitementExecution extends GenericTraitement implements ITraitement
{	
	public TraitementExecution() {}

	@Override
	public boolean traitement() 
	{
		retirerJusquaPos();
		String argument = null;
		String nomExecution = null;
		
		ETypeExecution type = rechercheExecution();
		if(type == null) 
		{
			return false;
		}
		retirerJusquaPos();
		
		nomExecution = getNomExecution();
		
		int pos = hasArgument();
		if(hasArgument() != -1)
		{
			this.posListe = pos;
			retirerJusquaPos();
		}
		else
		{
			this.termes.remove(0);
		}
		argument = getArgument();

		return lancerExecution(type, nomExecution, argument);
	}
	
	private boolean lancerExecution(ETypeExecution type, String nomExecution, String argument) 
	{
		if(type == null || nomExecution == null || nomExecution.length() == 0) return false;
		
		Processus pro = null;
		
		switch(type)
		{
			case BATCH :
				pro = this.console.executionShell(nomExecution, argument);
				break;
			case COMMANDE :
				pro = this.console.executionApp(nomExecution, argument);
				break;
			case FICHIER :
				pro = this.console.executionFichier(nomExecution, argument);
				break;
			case DOCUMENT :
				pro = this.console.executionFichier(nomExecution, argument);
				break;
			case PROGRAMME :
				pro = this.console.executionApp(nomExecution, argument);
				break;
			case SHELL :
				pro = this.console.executionShell(nomExecution, argument);
				break;
		}
		
		return pro != null;
	}

	private String getNomExecution() 
	{
		return this.termes.get(0);
	}
	
	private String getArgument() 
	{
		if(this.termes.size() == 0) return null;
		
		StringBuffer tmp = new StringBuffer();
		IIterateur<String> it = this.termes.iterator();
		
		while(it.hasNext())
		{
			tmp.append(it.next());
			if(it.hasNext())tmp.append(" ");
		}
		return tmp.toString();
	}

	private int hasArgument() 
	{
		IListeE<String> liste = PropertiesUtils.getValueList(this.propertiesComprehension, "argument");
		
		for(int i = 0; i < this.termes.size(); ++i)
		{
			for(int j = 0; j < liste.size(); ++j)
			{
				if(this.termes.get(i).contains(liste.get(j))) return i;
			}
		}
		return -1;
	}

	private ETypeExecution rechercheExecution()
	{
		int pos = this.termes.containsEquals(ETypeExecution.FICHIER.getValue(), false);
		if(pos != -1)
		{
			this.posListe = pos;
			return ETypeExecution.FICHIER;
		}
		pos = this.termes.containsEquals(ETypeExecution.DOCUMENT.getValue(), false);
		if(pos != -1)
		{
			this.posListe = pos;
			return ETypeExecution.DOCUMENT;
		}
		pos = this.termes.containsEquals(ETypeExecution.SHELL.getValue(), false);
		if(pos != -1)
		{
			this.posListe = pos;
			return ETypeExecution.SHELL;
		}
		pos = this.termes.containsEquals(ETypeExecution.BATCH.getValue(), false);
		if(pos != -1)
		{
			this.posListe = pos;
			return ETypeExecution.BATCH;
		}
		pos = this.termes.containsEquals(ETypeExecution.COMMANDE.getValue(), false);
		if(pos != -1)
		{
			this.posListe = pos;
			return ETypeExecution.COMMANDE;
		}
		pos = this.termes.containsEquals(ETypeExecution.PROGRAMME.getValue(), false);
		if(pos != -1)
		{
			this.posListe = pos;
			return ETypeExecution.PROGRAMME;
		}
	
		return null;
	}
	
	@Override
	public void setListe(IListeE<String> liste) {this.termes = liste;}
	@Override
	public void setPositionAction(int posAction) {this.posListe = posAction;}
}
